package edu.harding.redzone;

import java.io.File;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.ImageView;
import android.widget.Toast;

public class ViewModeActivity extends Activity
{

	private static final int SELECT_PICTURE = 1;

	private final int DIALOG_DELETE_ID = 1;
	private final int DIALOG_SELECT_ID = 2;
	private final int DIALOG_NO_IMAGE_ID = 3;

	private String mSelectedImagePath;

	private ImageView mImageView;
	private final String MIME = "image/png";

	private Toast mToast;

	private Intent mIntent;

	private File mFile;

	private Uri mUri;

	private boolean mNoUri;

	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.view_mode);

		selectImage();

		mImageView = (ImageView) findViewById(R.id.imageView);

	}

	@Override
	protected void onResume()
	{
		super.onResume();
		if (mSelectedImagePath == null && mNoUri == true)
			showDialog(DIALOG_NO_IMAGE_ID);
	}

	private void selectImage()
	{
		mIntent = new Intent();
		mIntent.setType(MIME);
		mIntent.setAction(Intent.ACTION_GET_CONTENT);
		startActivityForResult(Intent.createChooser(mIntent, "Select Picture"),
				SELECT_PICTURE);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu)
	{
		super.onCreateOptionsMenu(menu);
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.view_mode_menu, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item)
	{
		switch (item.getItemId())
		{
		case R.id.send:
			onSendMenuItemClicked();
			return true;
		case R.id.delete:
			if (mSelectedImagePath != null)
				showDialog(DIALOG_DELETE_ID);
			else
				showDialog(DIALOG_SELECT_ID);
			return true;
		case R.id.select:
			onSelectMenuItemClicked();
			return true;
		}
		return false;
	}

	private void onSelectMenuItemClicked()
	{
		selectImage();
	}

	private void onSendMenuItemClicked()
	{
		if (mSelectedImagePath != null)
		{
			mFile = new File(mSelectedImagePath);
			mUri = Uri.fromFile(mFile);

			mIntent = new Intent(android.content.Intent.ACTION_SEND);
			mIntent.putExtra(Intent.EXTRA_STREAM, mUri);
			mIntent.setType(MIME);
			startActivity(mIntent);
		} else
		{
			showDialog(DIALOG_SELECT_ID);
		}

	}

	private void showToast(String message, int duration)
	{
		mToast = Toast.makeText(getApplicationContext(), message, duration);

		mToast.setGravity(Gravity.TOP | Gravity.CENTER, 0, 0);
		mToast.show();
	}

	@Override
	protected Dialog onCreateDialog(int id)
	{
		Dialog dialog = null;
		AlertDialog.Builder builder = new AlertDialog.Builder(this);

		switch (id)
		{
		case DIALOG_DELETE_ID:
			builder.setMessage(R.string.delete_question)
					.setCancelable(false)
					.setPositiveButton(R.string.yes,
							new DialogInterface.OnClickListener()
							{
								public void onClick(DialogInterface dialog,
										int id)
								{
									showToast(getString(R.string.deleting),
											Toast.LENGTH_SHORT);
									mFile = new File(mSelectedImagePath);
									mFile.delete();
									showToast(getString(R.string.deleted),
											Toast.LENGTH_SHORT);
									mImageView
											.setImageResource(R.drawable.blank);
								}
							}).setNegativeButton(R.string.no, null);
			dialog = builder.create();
			break;
		case DIALOG_SELECT_ID:
			builder.setMessage(getString(R.string.select_string))
					.setCancelable(false)
					.setPositiveButton(R.string.ok,
							new DialogInterface.OnClickListener()
							{
								public void onClick(DialogInterface dialog,
										int id)
								{
									dialog.dismiss();
								}
							});
			dialog = builder.create();
			break;
		case DIALOG_NO_IMAGE_ID:
			builder.setMessage(getString(R.string.no_image_string))
					.setCancelable(false)
					.setPositiveButton(R.string.ok,
							new DialogInterface.OnClickListener()
							{
								public void onClick(DialogInterface dialog,
										int id)
								{
									dialog.dismiss();
								}
							});
			dialog = builder.create();
			break;
		}

		return dialog;
	}

	public void onActivityResult(int requestCode, int resultCode, Intent data)
	{
		if (resultCode == RESULT_OK)
		{
			if (requestCode == SELECT_PICTURE)
			{
				Uri selectedImageUri = data.getData();
				mSelectedImagePath = getPath(selectedImageUri);
				mImageView.setImageURI(Uri.parse(mSelectedImagePath));
				showToast(getString(R.string.viewmode_string),
						Toast.LENGTH_LONG);
			}
		} else
			mNoUri = true;
	}

	public String getPath(Uri uri)
	{
		String[] projection = { MediaStore.Images.Media.DATA };
		Cursor cursor = managedQuery(uri, projection, null, null, null);
		int column_index = cursor
				.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
		cursor.moveToFirst();
		return cursor.getString(column_index);
	}
}
